

import java.awt.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;




public class ManageSchool {
    SchoolBean[] sBeans=new SchoolBean[50];//数组添加位置
    Scanner scanner=new Scanner(System.in);
	//显示所有英雄
	public void showALL() {
		ArrayList<SchoolBean> aList=new ArrayList<SchoolBean>();
		String sqlString="select id,name,dment from employee";
		QueryDataResult rSet=DBManager.excuteQuery(sqlString);
			if (rSet!=null) {
				try {
					ResultSet rsSet=rSet.rs;
					while (rsSet.next()) {
						aList.add(new SchoolBean(rsSet.getInt("id"),rsSet.getString("name"),rsSet.getString("dment")));
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			rSet.close();
			System.out.println("编号\t姓名\t部门");
			for (SchoolBean pBean : aList) {
				System.out.println(pBean.getId()+"\t"+pBean.getSname()+"\t"+pBean.getDment());
			}
		}
	//判断id是否存在
	public SchoolBean selectById(int id) {
		String sql="select id,name,dment from employee where id="+id;
	    QueryDataResult queryDataResult =DBManager.excuteQuery(sql);
	    try {
	    	ResultSet rsSet=queryDataResult.rs;
			if (rsSet.next()) {
				return new SchoolBean(rsSet.getInt("id"),rsSet.getString("name"),rsSet.getString("dment"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	    return null;
	}
	
	//添加
	public void addEmp() {
		System.out.println("员工编号");
	    int id=scanner.nextInt();
	    SchoolBean pBean=selectById(id);
	    if (pBean!=null) {
			System.out.println("员工编号重复,请重新添加");
		}else {
			    System.out.println("请输入新员工的姓名:");
			    String name=scanner.next();
			    System.out.println("请输入新员工所在部门:");
			    String dment=scanner.next();
			    String sqlString="insert into employee(id,name,dment) values('"+id+"','"+name+"','"+dment+"')";
			    int result=DBManager.excuteUpdate(sqlString);
			    System.out.println(result>0?"添加成功":"添加失败");
		}
	    
	}
	//修改
	
	public void empUpdate() {
		System.out.println("请输入要改的员工编号");
		int id=scanner.nextInt();
		SchoolBean pBean=selectById(id);
		if (pBean==null) {
			System.out.println("员工编号不存在,无法修改");
		}else {
			String name=pBean.getSname();
			String dment=pBean.getDment();

			String sqlString="UPDATE employee SET id='"+id+"'";
			System.out.println("是否修改姓名 y/n");
			String str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入新员工的姓名:");
				name=scanner.next();
				sqlString=sqlString+",name='"+name+"'";
			}
			System.out.println("是否修改部门 y/n");
			str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入新员工所在部门:");
				sqlString=sqlString+",type='"+scanner.next()+"'";
			}
			sqlString=sqlString+"where id="+id;
			int result=DBManager.excuteUpdate(sqlString);
			System.out.println(result>0?"修改成功":"修改失败");
		}
		
	}

	//模糊查询
	public void showALL1() {
		ArrayList<SchoolBean> aList=new ArrayList<SchoolBean>();
		System.out.println("请输入姓名/姓氏/名");
		String name=scanner.next();
		String sql="select id,name,dment from employee where name"+" like '%"+name+"%'";
		QueryDataResult rSet=DBManager.excuteQuery(sql);
		if (rSet!=null) {
			try {
				ResultSet rsSet=rSet.rs;
				while (rsSet.next()) {
					aList.add(new SchoolBean(rsSet.getInt("id"),rsSet.getString("name"),rsSet.getString("dment")));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		rSet.close();
		System.out.println("编号\t姓名\t部门");
		for (SchoolBean pBean : aList) {
			System.out.println(pBean.getId()+"\t"+pBean.getSname()+"\t"+pBean.getDment());
		}
	}
}
